/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MemberChoosePanel.java
 *
 * Created on 2011-12-9, 22:15:33
 */
package mscs.client.member.view;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import mscs.client.member.logic.GiftServiceUtil;
import mscs.client.member.logic.GiftUtil;
import mscs.client.member.logic.IGiftServiceUtil;
import mscs.client.member.logic.IGiftUtil;
import mscs.client.view.MscsView;
import mscs.data.pojo.GiftRecord;
import mscs.data.pojo.Goods;
import mscs.data.pojo.Member;
import mscs.data.pojo.SaleRecord;

/**
 *
 * @author Administrator
 */
public class MemberChoosePanel extends javax.swing.JPanel {
    MscsView mscsView ;
    private DefaultTableModel tableModel1 = new DefaultTableModel();
    private DefaultTableModel tableModel2 = new DefaultTableModel();
    private IGiftServiceUtil giftServiceUtil = new GiftServiceUtil();
    private IGiftUtil giftutil = new GiftUtil();
    private List<Member> tobeSelectedMemberList = new ArrayList<Member>();
    private List<Member> selectedMemberList = new ArrayList<Member>();
    private List<Integer> tobeSelectedResoanList = new ArrayList<Integer>();
    private List<Integer> selectedReasonList = new ArrayList<Integer>();
    
    /** Creates new form MemberChoosePanel */
    public MemberChoosePanel(MscsView parent) {
        this.mscsView = parent;
        initComponents();
         this.tobeSelectedMemberList = this.giftServiceUtil.getGiftMemberList(giftutil, this.tobeSelectedResoanList);
        refreshTable();
    }
    
    public void refreshTable(){
        this.tableModel1.setDataVector(GiftTableModelFactory.toGiftMemerData(tobeSelectedMemberList, tobeSelectedResoanList), 
                GiftTableModelFactory.GIFT_MEMBER_COLUMN_NAMES);
        this.tableModel2.setDataVector(GiftTableModelFactory.toGiftMemerData(selectedMemberList, selectedReasonList), 
                GiftTableModelFactory.GIFT_MEMBER_COLUMN_NAMES);
        this.buttonSelect.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        buttonSelect = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mscs.client.view.MscsApp.class).getContext().getResourceMap(MemberChoosePanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(tableModel1);
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        buttonSelect.setText(resourceMap.getString("buttonSelect.text")); // NOI18N
        buttonSelect.setName("buttonSelect"); // NOI18N
        buttonSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSelectActionPerformed(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable2.setModel(tableModel2);
        jTable2.setName("jTable2"); // NOI18N
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(189, 189, 189)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                    .addComponent(buttonSelect))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                .addGap(9, 9, 9))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonSelect)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
    this.buttonSelect.setEnabled(true);
}//GEN-LAST:event_jTable1MouseClicked

private void buttonSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSelectActionPerformed
    Member member = this.tobeSelectedMemberList.get(this.jTable1.getSelectedRow());
    List<SaleRecord> recordList = this.giftServiceUtil.getMemberSaleList(member);//仍待测试
    MemberInfoDialog memberInfoDialog = new MemberInfoDialog(this.mscsView.getFrame(), true, member, recordList );
    member = memberInfoDialog.showDialog();
    
    if(member == null){
        refreshTable();
        return ;
    }
    List<Goods> giftList = this.giftServiceUtil.getAllGifts();
    GiftServiceDialog giftServiceDialog = new GiftServiceDialog(this.mscsView.getFrame(), true, giftList, member, 
            this.tobeSelectedResoanList.get(this.jTable1.getSelectedRow()));
   GiftRecord newRecord =  giftServiceDialog.showDialog();
   
    if(newRecord == null){
       refreshTable();
       return;
     }
    GiftCommitDialog commitDialog = new GiftCommitDialog(this.mscsView.getFrame(), true, newRecord);
    int index = this.jTable1.getSelectedRow();
    this.selectedMemberList.add(this.tobeSelectedMemberList.get(index));
    this.selectedReasonList.add(this.tobeSelectedResoanList.get(index));
    this.tobeSelectedMemberList.remove(index);
    this.tobeSelectedResoanList.remove(index);
    this.giftServiceUtil.commitGiftService(newRecord);
    this.refreshTable();
}//GEN-LAST:event_buttonSelectActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
